package com.zy.security;

import com.alibaba.fastjson.JSON;
import com.zy.pojo.Menu;
import java.util.ArrayList;
import java.util.List;

/**
 * 查询出全部权限--转为相应的结果测试
 * [
 *                 {
 *                     "path": "1",
 *                     "title": "工作台",
 *                     "icon":"fa-dashboard",
 *                     "children": []
 *                 },
 *                 {
 *                     "path": "2",
 *                     "title": "会员管理",
 *                     "icon":"fa-user-md",
 *                     "children": [
 *                         {
 *                             "path": "/2-1",
 *                             "title": "会员档案",
 *                             "linkUrl":"member.html",
 *                             "children":[]
 *                         },
 *                         {
 *                             "path": "/2-2",
 *                             "title": "体检上传",
 *                             "children":[]
 *                         },
 *                     ]
 *                 },
 *                 {
 *                     "path": "3",
 *                     "title": "预约管理",
 *                     "icon":"fa-tty",
 *                     "children": [
 *                         {
 *                             "path": "/3-1",
 *                             "title": "预约列表",
 *                             "linkUrl":"ordersettinglist.html",
 *                             "children":[]
 *                         },
 *                         {
 *                             "path": "/3-2",
 *                             "title": "预约设置",
 *                             "linkUrl":"ordersetting.html",
 *                             "children":[]
 *                         },
 *                         {
 *                             "path": "/3-3",
 *                             "title": "套餐管理",
 *                             "linkUrl":"setmeal.html",
 *                             "children":[]
 *                         },
 *                         {
 *                             "path": "/3-4",
 *                             "title": "检查组管理",
 *                             "linkUrl":"checkgroup.html",
 *                             "children":[]
 *                         },
 *                         {
 *                             "path": "/3-5",
 *                             "title": "检查项管理",
 *                             "linkUrl":"checkitem.html",
 *                             "children":[]
 *                         },
 *                     ]
 *                 }
 *             ]
 *
 *
 *      返回的数据机构设计： list<Menu>
 */
public class testSecurity {
	//将一个元素插入到list-out树中
	static List<Menu> insert(List<Menu> menus,Menu x){
		if(menus==null||menus.isEmpty()){
			return menus;
		}else{

			for (int i = 0; i < menus.size(); i++) {
				if(x.getParentMenuId()==menus.get(i).getId()){
					//当前节点
					menus.get(i).getChildren().add(x);
					break;
				}else{
					//查询子元素
					menus.get(i).setChildren(insert(menus.get(i).getChildren(),x));
				}
			}
		}
	return menus;
	}

	public static List<Menu> sort(List<Menu> menus){
		//把为空的去除掉--init
		List<Menu> out = new ArrayList<>();
		for (int i = 0; i < menus.size(); i++) {
			if(menus.get(i).getParentMenuId()==null){
				out.add(menus.get(i));
				menus.remove(i);
				i--;
			}
		}
		//一个个插入
		for (int i = 0; i < menus.size(); i++) {
			out=insert(out,menus.get(i));
		}
		return out;

	}


	public static void main(String[] args) {
		Menu menu = new Menu(1, "a", null);
		Menu menu1 = new Menu(2, "a1", 1);
		Menu menu2 = new Menu(3, "a2", 1);
		Menu menu3 = new Menu(4, "a11", 2);
		Menu menu4 = new Menu(5, "a12", 2);
		Menu menu5 = new Menu(6, "a2", null);

		List<Menu> out = new ArrayList<>();
		out.add(menu);
		out.add(menu1);
		out.add(menu2);
		out.add(menu3);
		out.add(menu4);
		out.add(menu5);
		System.out.println(sort(out).get(0).getChildren().get(0).getChildren().get(0).getId());
		System.out.println(JSON.toJSONString(out));

	}

}
